#!/bin/bash
#ffing.cn
echo -e "\e[33m 此脚本为获取log文件中，指定天数内的日志，并做导出 \e[0m"
echo -e "\e[33m 查询1天内，就是当天！  查询2两天内，就是昨天和今天！\e[0m"
echo -e "\e[33m 默认导出到家目录~/out/源文件名！\e[0m"
read -p "输入查询文件路径：" FILENAME
read -p "输入查询天数(整数)：" DAYS

#获取文件名
OUTFILE=`echo "$FILENAME"|awk -F '/' '{print $NF}'` 
echo "$OUTFILE"
#创建导出目录
OUTDIR=~/out
mkdir -p $OUTDIR
EX=_
NOWDATE=`date +%F_%T`
OUTFILEPATH=$OUTDIR/$OUTFILE$EX$NOWDATE
echo  > $OUTFILEPATH
#英文月份替换为数字
cat $FILENAME    |sed 's/\/Jan/\/01/g'|sed 's/\/Feb/\/02/g'|sed 's/\/Mar/\/03/g' >/tmp/tmp1.log
cat /tmp/tmp1.log|sed 's/\/Apr/\/04/g'|sed 's/\/May/\/05/g'|sed 's/\/Jun/\/06/g' >/tmp/tmp2.log
cat /tmp/tmp2.log|sed 's/\/Jul/\/07/g'|sed 's/\/Aug/\/08/g'|sed 's/\/Sept/\/09/g' >/tmp/tmp3.log
cat /tmp/tmp3.log|sed 's/\/Oct/\/10/g'|sed 's/\/Nov/\/11/g'|sed 's/\/Dec/\/12/g' >/tmp/tmp4.log


DAY=`echo "$DAYS-1"|bc`
for i in `seq 0 $DAY`
do
#日期类型，根据自己脚本进行格式转换
HISDATE=`date -d "-"$i" day" +%d/%m/%Y`
cat /tmp/tmp4.log |grep "$HISDATE" 
if [ $? -eq 0 ] ;then
echo "---------------------------------------------------------------------------"
cat /tmp/tmp4.log |grep "$HISDATE"  >> $OUTFILEPATH
echo "---------------------------------------------------------------------------" >>$OUTFILEPATH
fi
done
echo -e "\e[33m 查询结果导出路径:$OUTFILEPATH \e[0m"
